package dr.joketalker.controller;

import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import dr.joketalker.R;
import dr.joketalker.Debug.DebugManager;
import dr.joketalker.R.array;
import dr.joketalker.R.drawable;
import dr.joketalker.R.id;
import dr.joketalker.R.layout;
import dr.joketalker.controller.view.GridItem;
import dr.joketalker.controller.view.GridViewAdapter;
import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.JokeDAO;
import dr.joketalker.entity.Joke;
import dr.joketalker.model.PassData;
import dr.joketalker.model.SearchModel;

public class CategoryController extends MyController{

	
	
	protected GridView gdvCategory;
    protected GridViewAdapter gdvAdapterCategory = new GridViewAdapter(this);
	
    protected JokeDAO jokeDao = FactoryDAO.getJokeDao();
    
    protected SearchModel searchModel = SearchModel.GetInstance();
    
	@Override
	 public void onCreate(Bundle savedInstanceState) {
	 
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.category);


	        gdvCategory = (GridView)findViewById(R.id.gdvCategory);

	        GridViewAdapter gdvAdapter = new GridViewAdapter(this);
	        

	        List<GridItem> listGridItem = new LinkedList<GridItem>();
	        
	        String arrCategory[] = getResources().getStringArray(R.array.arrCategory);
	        
	        
	        for(String value: arrCategory){
	        	GridItem gi = new GridItem(this,value,R.drawable.ic_launcher);
	        	listGridItem.add(gi);
	        }
        
	        for(GridItem gi : listGridItem){
	        	gdvAdapter.addItem(gi);
	        }


	        gdvCategory.setOnItemClickListener(this);

	        gdvCategory.setAdapter(gdvAdapter);
	           
	 
	             
	        init();
	 }
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
		super.onItemClick(arg0, view, arg2, arg3);
		
		
		TextView tv = (TextView)view.findViewById(R.id.tvTitle);
		String category = tv.getText().toString(); 
		searchModel.clear();
		searchModel.setCategory(category);
		searchModel.readAll();
		

		Intent intent = new Intent(this, SearchController.class);
		
	
		startActivity(intent);
		
	}
	
}
